// pages/schoolSC/schoolSC.js
var request = require("../../utils/request.js")
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        times: 0,
        yzmMsg: "填入验证码",
        info: "请输入家长在学校预留的手机号",
        info1: " 获取学生的视力报告",
        s: "1", //bank 空白页类型 1显示 0隐藏
        mobile: "", //手机号
        ishidden: true, //判断list是否显示,初始化默认不显示
        reportList: [], //报告列表
        m:'0'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    wx.showShareMenu({ menus: ['shareAppMessage', 'shareTimeline']})
    },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage (res) {
    return {
      title: '迪佳极视',
      path: 'pages/index/index',
      // imageUrl:'这个是显示的图片，不写就默认当前页面的截图',
      success: function (shareTickets) {
        console.info(shareTickets + '成功');
        // 转发成功
      },
      fail: function (res) {
        console.log(res + '失败');
        // 转发失败
      },
      complete:function(res){
        // 不管成功失败都会执行
      }
    }
  },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.toastDialog = this.selectComponent("#toastDialog"); //此处id和页面的 wxml中的id一致
    },
    //获取input 输入手机号的值
    getInputValue: function (e) {
        this.setData({
            mobile: e.detail.value
        })
    },
    // 根据手机号查询报告列表
    getSearchStudentList: function () {
        console.log(this.data.mobile)
        const that = this
        const regex = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
        if (!regex.test(that.data.mobile) || that.data.mobile.length != 11) {
            this.toastDialog.showDialog({
                code: '1',
                message: "手机号码格式错误",
                timeout: true
            });
            return false
        }
        that.toastDialog.showDialog({
            code: 'loading',
            message: '加载中',
            timeout: false
        }); //loading 加载框
        request.postData('xcx/searchStudent', {
            mobile: that.data.mobile
        }, function (res) {
            // success
            that.toastDialog.hideDialog(); //关闭loading提示框
            if (res.code == '0') {
                res.reportList.forEach((item) => {
                    item.lockName = item.name.substring(0, 1) + '*' + item.name.substring(2, 1); //要截取字段的字符串
                    item.lockMobile = item.parentMobile.substr(0, 3) + '****' + item.parentMobile.substr(7, 4)
                })
                that.setData({
                    reportList: res.reportList,
                    s: '0',
                    ishidden: 'false'
                })
                console.log(that.data.reportList)
            } else {
                that.setData({
                    info: '很抱歉！未查询到相关视力报告。',
                    info1: '',
                    s: '1',
                    ishidden: true,
                    reportList: []
                })
            }

        }, function (res) {
            that.toastDialog.showDialog({
                code: res.code,
                message: res.message,
                timeout: true
            });
        });
    },
    //查看档案 按钮
    getDetail: function (e) {
        let item = e.currentTarget.dataset.item
        let that = this
        if (item.unlock == '0') {
            console.log(item)
            wx.navigateTo({
                url: '../VerifyMobile/VerifyMobile?mobile='+item.parentMobile +'&id='+item.id
            })
        }else if (item.unlock == '1'){
            wx.navigateTo({
                url: '../seeArchives/seeArchives?mobile='+item.parentMobile+'&studentId='+item.id
              })
        }
    },
     //上传头像
  chooseImg:function(option){
    var that = this;
    let item = option.currentTarget.dataset.item
    let idx = option.currentTarget.dataset.index;
    wx.chooseImage({
      count: 1,
      success: function (e) {
        wx.uploadFile({
          url: 'https://wxxcx.di.plus/djeye/xcx/updateStudentPhoto',
          filePath: e.tempFilePaths[0],
          header: { "openId": wx.getStorageSync("openid") }, 

          name: 'photo',
          formData: {
            'studentId': item.id
          },
          success: function (res) {
            //console.log(res)
            that.data.reportList[idx].photo = e.tempFilePaths[0]
            //console.log(that.data.reportList);
            that.setData({
              reportList:that.data.reportList
            })
            
          }, fail: function (err) {
            console.log(err)
          }
        });
      },
      fail: function (e) {

      }
    })
  }
})